//
//  JLDistrictViewController.m
//  Liang美团HD
//
//  Created by sun on 16/1/21.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "JLDistrictViewController.h"
#import "JLCityViewController.h"
#import "JLNavigationController.h"
#import "UIBarButtonItem+JLBarButtonItem.h"
#import "JLData.h"
#import "JLCities.h"
#import "JLDistricts.h"
static NSString * const ID = @"districtsCell";
@interface JLDistrictViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;
@property (weak, nonatomic) IBOutlet UIButton *districtBtn;

@end

@implementation JLDistrictViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat rowHeight = 40;
    self.leftTableView.rowHeight = rowHeight;
    self.rightTableView.rowHeight = rowHeight;
    self.preferredContentSize = CGSizeMake(400, 400);
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.leftTableView.showsVerticalScrollIndicator = NO;
    self.rightTableView.showsVerticalScrollIndicator = NO;
    }

- (IBAction)districtBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    UITabBarController * tabBarVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    JLCityViewController * cityVC = [[JLCityViewController alloc]init];
    JLNavigationController * navVC = [[JLNavigationController alloc]initWithRootViewController:cityVC];
    navVC.modalPresentationStyle = UIModalPresentationFormSheet;
    [tabBarVC presentViewController:navVC animated:YES completion:nil];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.leftTableView) {
        return  self.districts.count;
    }else{
        NSUInteger row = [self.leftTableView indexPathForSelectedRow].row;
        JLDistricts * dis = self.districts[row];
        return dis.subdistricts.count ;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
    }
    if (tableView == self.leftTableView) {
        JLDistricts * districts = self.districts[indexPath.row];
         cell.textLabel.text = districts.name;
    }else{
        NSUInteger row = [self.leftTableView indexPathForSelectedRow].row;
        JLDistricts * districts = self.districts[row];
        cell.textLabel.text = districts.subdistricts[indexPath.row];
    }
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.leftTableView) {
        [self.rightTableView reloadData];
        if (0 == indexPath.row) {
            [[NSNotificationCenter defaultCenter] postNotificationName:JLDistrictsDidChangeNotification object:self.districts[indexPath.row]];
        }
    }else{
        NSUInteger row = [self.leftTableView indexPathForSelectedRow].row;
        JLDistricts * districts = self.districts[row];

        [[NSNotificationCenter defaultCenter] postNotificationName:JLDistrictsDidChangeNotification object:districts userInfo:@{JLDistrictsIndexFromTableViewDidClickKey:@(indexPath.row)}];
    }
    
}
@end
